package com.hnkywl.sts.dao.base;

import com.hnkywl.sts.entity.base.Dxrz;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-20
 * Time: 下午8:48
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class DxrzDao extends SimpleHibernateTemplate<Dxrz, Long> {
    public Page<Dxrz> findDxrzList(Date fsrq,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Dxrz> page = new Page<Dxrz>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (fsrq != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" (fsrq >= ? ");
            condition.append(" and ");
            condition.append(" fsrq < ?) ");
            param.add(fsrq);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(fsrq);
            calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
            Date fsrqEnd=calendar.getTime();
            param.add(fsrqEnd);
        }
        //==============记录数量===============
        hql = "select count(*) from Dxrz";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Dxrz ";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        hql = hql + " order by lastUpdated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }
}
